(function ($) {
	var sourceData;
	var perInfo,pageType,pageData,nurseid
	var unUploadFiles=[] //未上传的图片
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	function submitNewData(){
		// plus.webview.getWebviewById('../WKPlatformPages/teach/teachList.html').evalJS('detailChanged()')
		// mui.back()
		// return;
		blurAllInput()
		var status = this.getAttribute('data-id');
		var parr = ''
		var domArr = pageData['parrArr']
		for (var i = 0; i < domArr.length; i++) {
			var oneDom = domArr[i]
			var dom = $('#'+oneDom['domId'])[0]
			var domValue = dom.getAttribute('data-id')
			if (oneDom['type'] == 'date') {
				domValue = dom.innerText
			}else if (oneDom['type'] == 'input') {
				domValue = dom.value
			}
			if (!domValue) {
				mui.toast(oneDom['tip'])
				return
			}
			parr += domValue +'^'
		}
		parr +=status+'^'
		if (sourceData) {
			parr += sourceData['rw']
		}
		//console.log(parr)
		var pdict = {'nurseid':nurseid,'parr':parr}
		var paramDict = {'method':'','params':JSON.stringify(pdict),'type':'Method'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var msg = parseInt(result['msg'])
			if (msg == 0) {
				mui.toast('提交失败')
				return 
			}
			if (unUploadFiles.length==0 ) {
				plus.webview.getWebviewById('../WKPlatformPages/teach/teachList.html').evalJS('detailChanged()')
				mui.back()
				return;
			}
			var path = getUploadFileToServerPath(pageType)
			var method = getSaveFileMethod(pageType)
			var paramDict={'id':msg,'path':path,'imgArr':unUploadFiles,'method':method}
			saveFileAction(paramDict,function(flag){
				if (!flag) {
					return
				}
				unUploadFiles=[]
				plus.webview.getWebviewById('../WKPlatformPages/teach/teachList.html').evalJS('detailChanged()')
				mui.back()
			})
		}, function(errorStr) {
			mui.toast('请重试')
		});
	}
	
	function updateUI(){
		var domArr = pageData['parrArr']
		var fileKey = ''
		for (var i = 0; i < domArr.length; i++) {
			var oneDom = domArr[i]
			var dom = $('#'+oneDom['domId'])[0]
			if (oneDom['type'] == 'date') {
				dom.innerText=sourceData[oneDom['key']]
			}else if (oneDom['type'] == 'input') {
				dom.value=sourceData[oneDom['key']]
			}else{
				var value = sourceData[oneDom['key']+'Desc']
				if (oneDom['domId'] == 'persons' || oneDom['domId'] == 'StandardParticipant') {
					value = sourceData[oneDom['key']+'Name']
				}
				dom.innerText=value
				dom.setAttribute('data-id',sourceData[oneDom['key']])
			}
		}
		updateImageFileUI('BfPic','fileUL',sourceData,sourceData['rw'],'LocInn')
		updateFileUI(fileKey,'fileUL',sourceData,sourceData['rw'],getUploadFileToServerPath(pageType))
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var self = plus.webview.currentWebview()
		sourceData = self.data
		pageType = self.pageType
		if (pageType == '业务学习登记') {
			$('#UL')[0].classList.add('YW')
			pageData = [{'domId':'applyLoc','tip':'请选择病区','key':'BusLoc'},
				{'domId':'date','tip':'请选择日期','key':'ActivityNurse'},
				{'domId':'ActivityType','tip':'请选择活动形式','key':'ActivityType'},
				{'domId':'level','tip':'请选择级别','key':'ActivityLevel'},
				{'domId':'date','tip':'请选择日期','type':'date','key':'ActivityDate'},
				{'domId':'Topic','tip':'请输入活动主题','type':'input','key':'ActivityTopic'},
				{'domId':'Location','tip':'请输入活动地点','type':'input','key':'ActivityLocation'}]
		}else{
			pageData = [{'domId':'Ward','tip':'请选择日期','key':'ActivityWard'},
				{'domId':'persons','tip':'请选择参与人','key':'ActivityNurse'},
				{'domId':'ActivityType','tip':'请选择活动形式','key':'ActivityType'},
				{'domId':'ParType','tip':'请选择参与形式','key':'ActivityParType'},
				{'domId':'level','tip':'请选择级别','key':'ActivityLevel'},
				{'domId':'date','tip':'请选择日期','type':'date','key':'ActivityDate'},
				{'domId':'Topic','tip':'请输入活动主题','type':'input','key':'ActivityTopic'},
				{'domId':'Location','tip':'请输入活动地点','type':'input','key':'ActivityLocation'}]
		}
		if (sourceData) { //修改
			updateUI()
		}
		perInfo = self.perInfo
		nurseid = perInfo['loginID']
		$('#CreatorName')[0].innerText = perInfo['loginName']
		$('#applyLoc')[0].innerText = perInfo['loginWardDesc']
		$('#actor')[0].innerText = perInfo['loginName']
		$('#actor')[0].setAttribute('data-id',perInfo['loginID'])
		//关闭按钮
		mui('body').on('tap','#closeBtn',mui.back)
		//保存提交按钮
		mui('body').on('tap','.submitBtn',submitNewData)
		//参与人
		mui('body').on('tap','.authType',function(){
			blurAllInput()
			var dom = this
			setTimeout(function() {
				toChosePerson(dom.id,'多选') //防止切换页面时键盘未隐藏
			}, 100);
		});
		//下拉列表
		mui('#UL').on('tap','.listType',downListElementTapped);
		// 日期
		mui('#UL').on('tap','.dateType',selectDate);
		window.addEventListener('resize',windowResize)
		
		if (pageType != '业务学习登记') {
			return
		}
		$('#addFileBtn').on('change','input',function(){
			var imgArr = this.files
			unUploadFiles = afterSelectFile('fileUL',unUploadFiles,imgArr)
		});
		//监听文件删除按钮
		mui('#fileUL').on('tap','.mui-btn',function(){
			var method = getDeleteFileMethod(pageType)
			var paramDict={'imgArr':unUploadFiles,'method':method,'nurseid':nurseid}
			deleteFileBtnTapped(this,paramDict,function(flag,arr){
				if (arr) {
					unUploadFiles = arr
				}
			});
		}) 
		mui.back = function(){
			if (unUploadFiles.length > 0) {
				mui.confirm('您有未上传的文件','温馨提示',['放弃','继续操作'],function (e) {
					if (e.index == 0) {
						plus.nativeUI.closeWaiting()
						plus.webview.currentWebview().close()
					}
				},'div')
				return
			}
			plus.nativeUI.closeWaiting()
			plus.webview.currentWebview().close()
		}
	}
	
})(mui)

function selectPerson(domId,params){
	setAuthorDomInfo(domId,params)
	mui('#realnum')[0].innerText = params.length
}
